package yl.hs.bmipfull.controllers.material;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import yl.hs.bmipfull.controllers.ControllerBase;
import yl.hs.bmipfull.service.material.ISupplierService;
import yl.hs.bmipfull.service.system.ISearchViewService;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.SubmitModel;
import yl.hs.bmipfull.viewmodels.WebResult;

/**
 * 作用:   供应商信息控制层<br/>
 * 创建时间: 2021-05-18 16:42 <br/>
 *
 * @author 吴高丰
 */
@Api(value = "供应商信息web接口", tags = "采购管理——供应商信息表接口")
@RequestMapping(value = "/api/purchase/supplier")
@RestController
public class SupplierController extends ControllerBase {
    @Autowired
    ISupplierService service;
    @Autowired
    ISearchViewService svSVC;

    @ApiOperation(value = "查询供应商信息", notes = "分页查询供应商信息")
    @RequestMapping(value = "/{rowNum}/{page}", method = RequestMethod.POST)
    public WebResult queryPage(@RequestBody PageQuery query,
                               @ApiParam(value = "页码") @PathVariable int page,
                               @ApiParam(value = "每页条数") @PathVariable int rowNum) {
        try {
            var result = service.queryPage(query, rowNum, page);
            svSVC.updateUserMenuView(query);
            return success("ok", result);
        } catch (Exception ee) {
            return error(ee);
        }
    }

    @ApiOperation(value = "保存供应商信息", notes = "保存供应商信息")
    @RequestMapping(value = "", method = RequestMethod.PUT)
    public WebResult saveSupplier(@ApiParam(value = "提交信息") @RequestBody SubmitModel submitModel) {
        Integer result;
        try {
            result = service.saveSupplier(submitModel.getData(), submitModel.getSn());
            if (result > 0) {
                return success("提交成功");
            } else if (result == -1) {
                return error("供应商已存在");
            } else {
                return fail("数据未保存");
            }
        } catch (Exception e) {
            return error(e);
        }
    }

    @ApiOperation(value = "根据sn删除供应商", notes = "根据sn删除供应商")
    @RequestMapping(value = "", method = RequestMethod.DELETE)
    public WebResult delete(@ApiParam(value = "原材信息编号", name = "sn", allowMultiple = true, required = true) @RequestParam("sn") String[] sn) {
        if (service.delete(sn) > 0) {
            return success("删除成功");
        } else {
            return fail("删除失败");
        }

    }
    @ApiOperation(value = "根据编号修改状态", notes = "根据编号修改状态")
    @RequestMapping(value = "/changeStateBySn/{sn}/{state}", method = RequestMethod.GET)
    @ResponseBody
    public WebResult changeStateBySn(@PathVariable String sn,@PathVariable short state) {
        try {
            Integer result = service.changeStateBySn(sn.split(","), state);
            if (result > 0) {
                return success("数据已修改");
            } else if (result < -2) {
                return fail("数据修改失败！");
            }
            return fail("没有修改的任何数据");
        } catch (Exception ee) {
            return error(ee);
        }
    }
    @ApiOperation(value = "根据sn查询供应商", notes = "根据sn查询供应商")
    @RequestMapping(value = "/findsn/{sn}", method = RequestMethod.GET)
    public WebResult findBySn(@ApiParam(value = "供应商编号", name = "sn", allowMultiple = true, required = true) @RequestParam("sn") String sn) {
       try {
           var result = service.findBySn(sn);
           return success("查询成功", result);
       }catch (Exception ee){
           return fail(ee.getMessage());
       }
    }
}
